import {
  Column,
  Entity,
  OneToMany,
  ManyToMany,
  ManyToOne,
  JoinTable
} from 'typeorm'
import { ApiProperty } from '@nestjs/swagger'

import {
  BaseModelsWithUUIDPrimary,
  UploadEntity,
  CategoryEntity,
  UserEntity,
  PostCommentEntity
} from './index'

/**
 * @package 商品详情
 * @author liu
 */
@Entity({ name: 'post' })
export class PostEntity extends BaseModelsWithUUIDPrimary {
  @ApiProperty({ example: '标题', description: '标题' })
  @Column({ type: 'text', comment: '标题' })
  title: string

  @ApiProperty({
    example: '<div><p>吾日三省吾身，吾没钱 吾没车 吾没房。</p></div>',
    description: '内容'
  })
  @Column({ type: 'text', comment: '内容' })
  content: string

  @ApiProperty({ example: 1, description: '类型 1 图文, 2 视频' })
  @Column({ type: 'int', default: 1, comment: '类型' })
  type: number

  @ApiProperty({ example: 1, description: '是否可见' })
  @Column({ type: 'int', default: 1, comment: '是否可见' })
  status: number

  @ApiProperty({ example: [], description: '图片集', type: () => Array })
  @ManyToMany(type => UploadEntity)
  @JoinTable()
  cover: UploadEntity[]

  @ApiProperty({
    example: UserEntity,
    description: '作者',
    type: () => UserEntity
  })
  @ManyToOne(type => UserEntity, user => user.post)
  user: UserEntity

  /** 评论数 */
  commentsLength: number

  /** 评论列表 */
  @OneToMany(type => PostCommentEntity, comment => comment.post)
  comments: PostCommentEntity[]

  /** 分类 */
  @ApiProperty({ example: [], description: '分类', type: () => Array })
  @ManyToMany(type => CategoryEntity, category => category.post)
  @JoinTable({ name: 'post_category_post' })
  category: CategoryEntity[]

  /** 收藏商品的用户列表 */
  @ManyToMany(type => UserEntity, user => user.likePosts)
  likeUsers: UserEntity[]

  /** 点赞商品的用户列表 */
  @ManyToMany(type => UserEntity, user => user.starPosts)
  starUsers: UserEntity[]
}
